package lex.mtd.core.utils;

import java.io.File;
import java.io.IOException;

import org.lex.utils.Pair;


public class FileUtils
{
	private static final int MAX = 10;

	/**
	 * �����ļ�
	 * 
	 * @param file
	 * @return Pair(�����Ƿ�ɹ����, ������Ϣ)
	 */
	public static Pair<Boolean, IOException> createNewFile( File file )
	{
		if( file.exists() && file.isFile() )
			return Pair.wrap( true, null );
		else
		{
			boolean stop = false;
			int count = 0;
			IOException lastException = null;
			while( false == stop )
			{
				try
				{
					file.createNewFile();
					return Pair.wrap( true, null );
				}
				catch( IOException e )
				{
					try
					{
						Thread.sleep( 50 );
					}
					catch( InterruptedException e1 )
					{}
					e.printStackTrace();
					lastException = e;
				}
				count++;
				if( count > MAX )
					stop = true;
			}
			return Pair.wrap( false, lastException );
		}
	}
	
	public static void deleteFile(File file)
	{
		if( null == file || false == file.exists() )
			return;
		if( false == file.delete() )
			file.deleteOnExit();
	}
}
